﻿using IHotelDal;
using Microsoft.Data.SqlClient;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Data.SqlClient;
using System.Security.Cryptography;

namespace HotelDal
{
    public class PersonnelDal : IpersonnelDal
    {
        /// <summary>
        /// 新增员工
        /// </summary>
        /// <param name="S"></param>
        /// <returns></returns>
        public int AddStaff(Staffs S)
        {
            using (MyDbcontext Db = new MyDbcontext())
            {
                Db.Staffs.Add(S);
                return Db.SaveChanges();
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int AlterStaff(int id)
        {
            using (MyDbcontext Db = new MyDbcontext())
            {
                var Aid = Db.Staffs.Find(id);
                Db.Entry<Staffs>(Aid).State = EntityState.Modified;
                return Db.SaveChanges();
            }
        }

        /// <summary>
        /// 删除员工
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelStaff(int id)
        {
            using (MyDbcontext Db = new MyDbcontext())
            {
                var Did = Db.Staffs.Find(id);
                Db.Staffs.Remove(Did);
                return Db.SaveChanges();
            }
        }
        /// <summary>
        /// 部门管理
        /// </summary>
        /// <returns></returns>
        public List<Departments> DepartmentList()
        {
            using (MyDbcontext Db = new MyDbcontext())
            {

                var list = Db.Departments.ToList();
                return list;
            }
        }
        public int AddDpm(Departments s)
        {
            using (MyDbcontext Db = new MyDbcontext())
            {
                Db.Departments.Add(s);
                return Db.SaveChanges();
            }
        }
        public int Addps(Positions s)
        {
            using (MyDbcontext Db = new MyDbcontext())
            {
                Db.Positions.Add(s);
                return Db.SaveChanges();
            }
        }

        /// <summary>
        /// 职称管理
        /// </summary>
        /// <returns></returns>
        public List<Positions> PositionList( int id)
        {
            using (MyDbcontext Db = new MyDbcontext())
            {
                List<Positions> list = new List<Positions>();
                if (id > 0)
                {
                    list = (from c in Db.Positions where c.Department == id select c).ToList();
                    return list;
                }
                list = Db.Positions.ToList();

                return list;
            }
        }

        public List<Staffs> Show()
        {
            using (MyDbcontext Db = new MyDbcontext())
            {

                List<Staffs> list = (from a in Db.Staffs
                                     join b in Db.Positions on a.SPosition equals b.Pid
                                     join c in Db.Departments on b.Department equals c.Did
                                     select new Staffs
                                     {
                                         STaffID = a.STaffID,
                                         SSerial = a.SSerial,
                                         SName = a.SName,
                                         SSex = a.SSex,
                                         SAge = a.SAge,
                                         SRemark = a.SRemark,
                                         Position = b.Position,
                                         Department = c.Department
                                     }).ToList();


                return list;
            }
        }
    }
}
